<?php

namespace addons\unishop\controller;

use app\common\controller\Api;
use think\Cache;
use think\Db;
use think\Lang;

/**
 * 基础类
 * @ApiInternal
 */
class Backorder extends Api
{
    /**
     * 无需鉴权的方法,但需要登录
     * @var array
     */
    protected $noNeedRight = ['*'];
    protected $noNeedLogin = ['backorderdemo'];

    /**
     *  全部分类信息
     * */

    public function getbackallcate() {


       $list=Db::name('backcate')->order('weigh desc')->select();

       $this->success('分类信息',$list);

    }


    /**
     *  全部分类信息
     * */

    public function getbackgoods() {


        $backcate_id=$this->request->param('backcate_id');

        $map['backcate_id']=['eq',$backcate_id];

        $name=$this->request->param('name');
        if ($name){
            $map['name']=['like','%'.$name.'%'];
        }


        $list=Db::name('backgood')->where($map)->select();

        $this->success('分类商品信息',$list);

    }

    /**
     * 手机详情
     */

    public function getbackgoodbyid(){

        $id=$this->request->param('id');


        $info=Db::name('backgood')->where('id','eq',$id)->find();



        $lists=[];

        $model=$info['model'];
        if (strlen($model)>9){

            $models=[];
            $model=json_decode($model,true);
            foreach ($model  as $key=>$v){
                $vs['key']=$key;
                $vs['value']=$v;
                $models[]=$vs;
            }

            $lists[0]['title']='型号';
            $lists[0]['options']=$models;
        }


        $model=$info['memory']; //内存
        if (strlen($model)>9){

            $models=[];
            $model=json_decode($model,true);
            foreach ($model  as $key=>$v){
                $vs['key']=$key;
                $vs['value']=$v;
                $models[]=$vs;
            }

            $lists[1]['title']='内存';
            $lists[1]['options']=$models;
        }

        $model=$info['color']; //颜色
        if (strlen($model)>9){

            $models=[];
            $model=json_decode($model,true);
            foreach ($model  as $key=>$v){
                $vs['key']=$key;
                $vs['value']=$v;
                $models[]=$vs;
            }

            $lists[2]['title']='颜色';
            $lists[2]['options']=$models;
        }

        $model=$info['buy']; //购买渠道
        if (strlen($model)>9){

            $models=[];
            $model=json_decode($model,true);
            foreach ($model  as $key=>$v){
                $vs['key']=$key;
                $vs['value']=$v;
                $models[]=$vs;
            }

            $lists[3]['title']='购买渠道';
            $lists[3]['options']=$models;
        }


        $model=$info['warranty']; //保修信息
        if (strlen($model)>9){

            $models=[];
            $model=json_decode($model,true);
            foreach ($model  as $key=>$v){
                $vs['key']=$key;
                $vs['value']=$v;
                $models[]=$vs;
            }

            $lists[4]['title']='保修信息';
            $lists[4]['options']=$models;
        }

        $model=$info['other']; //有无以下情况
        if (strlen($model)>9){

            $models=[];
            $model=json_decode($model,true);
            foreach ($model  as $key=>$v){
                $vs['key']=$key;
                $vs['value']=$v;
                $models[]=$vs;
            }

            $lists[5]['title']='有无以下情况';
            $lists[5]['options']=$models;
        }

        $model=$info['machine']; //机身外观
        if (strlen($model)>9){

            $models=[];
            $model=json_decode($model,true);
            foreach ($model  as $key=>$v){
                $vs['key']=$key;
                $vs['value']=$v;
                $models[]=$vs;
            }

            $lists[6]['title']='机身外观情况';
            $lists[6]['options']=$models;
        }

        $model=$info['screen']; //屏幕信息
        if (strlen($model)>9){

            $models=[];
            $model=json_decode($model,true);
            foreach ($model  as $key=>$v){
                $vs['key']=$key;
                $vs['value']=$v;
                $models[]=$vs;
            }

            $lists[7]['title']='屏幕信息';
            $lists[7]['options']=$models;
        }

        $model=$info['repair']; //维修情况
        if (strlen($model)>9){

            $models=[];
            $model=json_decode($model,true);
            foreach ($model  as $key=>$v){
                $vs['key']=$key;
                $vs['value']=$v;
                $models[]=$vs;
            }

            $lists[8]['title']='维修情况';
            $lists[8]['options']=$models;
        }

        $model=$info['battery']; //电池情况
        if (strlen($model)>9){

            $models=[];
            $model=json_decode($model,true);
            foreach ($model  as $key=>$v){
                $vs['key']=$key;
                $vs['value']=$v;
                $models[]=$vs;
            }

            $lists[9]['title']='电池情况';
            $lists[9]['options']=$models;
        }

        $model=$info['mbuy']; //是否本店购买
        if (strlen($model)>9){

            $models=[];
            $model=json_decode($model,true);
            foreach ($model  as $key=>$v){
                $vs['key']=$key;
                $vs['value']=$v;
                $models[]=$vs;
            }

            $lists[10]['title']='是否本店购买';
            $lists[10]['options']=$models;
        }

        $model=$info['more']; //有无其他情况
        if (strlen($model)>9){

            $models=[];
            $model=json_decode($model,true);
            foreach ($model  as $key=>$v){
                $vs['key']=$key;
                $vs['value']=$v;
                $models[]=$vs;
            }

            $lists[11]['title']='有无其他情况';
            $lists[11]['options']=$models;
        }


        $info['list']=$lists;


        $this->success('手机详情',$info);

    }

    /**
     * 提交订单
     */

    public function submits(){

        $user=$this->auth->getUser();

        $log['backgood_id']=$this->request->param('backgood_id');
        $log['info']=$this->request->param('info');
        $log['money']=$this->request->param('money');
        $log['amount']=$this->request->param('amount');
        $log['paytype']=$this->request->param('paytype');
        $log['payaccount']=$this->request->param('payaccount');
        $log['payaname']=$this->request->param('payaname');
        $log['mobile']=$this->request->param('mobile');
        $log['imel']=$this->request->param('imel');
        $log['desc']=$this->request->param('desc');
        $log['time']=time();
        $log['user_id']=$user['id'];
        $log['bankname']=$this->request->param('bankname');

        $id=Db::name('backorder')->insertGetId($log);

        $this->success('订单创建成功',['id'=>$id]);

    }


    /**
     * 展示订单
     */

    public function backorderdemo(){


        $list=Db::name('backorderdemo')->order('weigh','desc')->select();


        $this->success('展示订单',$list);

    }


    /**
     * 订单列表
     */

    public function getallorder(){

        $user=$this->auth->getUser();
        $map['user_id']=['eq',$user['id']];


        $status=$this->request->param('status');
        if ($status){
            $map['status']=['eq',$status];
        }


        $list=Db::name('backorder')
            ->where($map)
            ->order('id','desc')->select();


        foreach ($list as &$key){
            $key['time']=date('Y-m-d H:i',$key['time']);
            $good=Db::name('backgood')->where(['id'=>$key['backgood_id']])->find();
            $key['g_name']=$good['name'];
        }


        $this->success('展示订单',$list);


    }

    /**
     * 订单详情
     */

    public function getordreinfobyid(){
        $id=$this->request->param('id');

        $list=Db::name('backorder')
            ->where('id','eq',$id)
            ->find();

        $list['time']=date('Y-m-d H:i',$list['time']);
        $good=Db::name('backgood')->where(['id'=>$list['backgood_id']])->find();
        $list['g_name']=$good['name'];

        $this->success('订单详情',$list);

    }


    /**
     * 订单 123
     */
    public function express(){
        $id=$this->request->param('id');
        $express=$this->request->param('express');

        Db::name('backorder')
            ->where('id','eq',$id)
            ->update(['express1'=>$express,'status'=>1]);


        $this->success('成功');
    }

}

